/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplerecipes;

import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.math.fraction.Fraction;
import org.apache.commons.math.fraction.FractionConversionException;
import simplerecipes.model.MesuredIngredient;
import simplerecipes.model.Recipe;
import simplerecipes.model.Unit;

/**
 *
 * @author matamh0a
 */
public class MesuredIngredientAdaptor {
    
    MesuredIngredient ingredient;
    Recipe recipe;
    DecimalFormat df = new DecimalFormat("#.##");
    
    public MesuredIngredientAdaptor(MesuredIngredient ingredient, Recipe recipe) {
        this.ingredient = ingredient;
        this.recipe = recipe;
    }

    public MesuredIngredient getIngredient() {
        return ingredient;
    }

    public void setIngredient(MesuredIngredient ingredient) {
        this.ingredient = ingredient;
    }
    
    public Object getValueAt(int columnIndex, Unit inUnit) {
        switch(columnIndex) {
            case 0: //included
                return new Boolean(ingredient.isIncluded());
            case 1: //Quantity
                return toFraction(ingredient.getQuantity());
            case 2: //Unit
                if(ingredient.getIngredient().getAlternativeUnit() != null)
                    return ingredient.getIngredient().getAlternativeUnit();
                return ingredient.getUnit();
            case 3://percentage
                return df.format(ingredient.getPercentage() * 100);
            case 4: //weight
                return df.format(ingredient.getWeight() / recipe.getToGramFactor());
            case 5:
                //return ingredient.getIngredient().getName();
                return ingredient.getIngredient().getName() + 
                        (ingredient.getDetails().length() > 0?"; ":"")
                        + ingredient.getDetails();
            case 6:
                    return df.format(ingredient.getIngredient().getWeight(
                        ingredient.getUnit()) / recipe.getToGramFactor());
            case 7: 
                    return df.format(ingredient.getIngredient().getCalories(
                        ingredient.getUnit()));
            
            case 8: //Calories
                return df.format(ingredient.getCalories());
            case 9: //Calories(%)
                return df.format(ingredient.getCalories() / recipe.getTotalCalories() * 100);
            case 10: //Calories per serving
                return df.format(ingredient.getCalories() / recipe.getServings());
            case 11: //Cost
                return df.format(ingredient.getCost() / recipe.getToGramFactor());
        }
        
        return "xxx";
    }

    public static String toFraction(double q) {
        double epsi = .125;
        boolean frac = Math.abs(q - ((int) q)) > epsi;


        try {
//            System.out.println("frac=" + frac + ";" + q  + ";" +  (q - ((int) q)) + "\t"
//                    + new Fraction(q - ((int) q), epsi, 10).toString().
//                    replace(" ", ""));

            if (new Fraction(q - ((int) q), epsi, 10).toString().
                    replace(" ", "").equals("0")) {
                q = (int) q;
                frac = false;
            } else if (new Fraction(q - ((int) q), epsi, 10).toString().
                    replace(" ", "").equals("1")) {
                q = (int) q + 1;
                frac = false;
            }


            return !frac ? (int) q + ""
                    : ((int) q == 0 ? "" : (int) q + " ")
                    + new Fraction(q - ((int) q), .125, 10).toString().
                    replace(" ", "");
        } catch (FractionConversionException ex) {
            Logger.getLogger(MesuredIngredientAdaptor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return q + "";
    }
    
    public static double toDecimal(String frac) {
        
        double r = 0.0;
        
        try {
            String[] n = frac.split(" ");

            //One part only
            if(n.length == 1) {
                if(frac.contains("/")) {
                    n = frac.split("/");
                    r = Double.parseDouble(n[0])/ Double.parseDouble(n[1]);
                } else {
                    r = Double.parseDouble(n[0]);
                }
                return r;
            }

            //two parts
            r = Double.parseDouble(n[0]);

            if(n.length == 2) {
                n = n[1].split("/");
                r += Double.parseDouble(n[0])/ Double.parseDouble(n[1]);
            }
        } catch (NumberFormatException nfe) {
            System.out.println("NumberFormatException: " + frac);
        }
        
        return r;
    }
}
